package com.yupi.yupao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yupao.model.domain.Message;
import com.yupi.yupao.model.domain.User;
import com.yupi.yupao.service.MessageService;
import com.yupi.yupao.mapper.MessageMapper;
import com.yupi.yupao.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *发送信息相关
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService{

    @Resource
    MessageMapper messageMapper;

    //    发送信息
    @Override
    public boolean insertMessage(Message message) {
        boolean save = this.save(message);
        return save;
    }

//    根据接收人id和发送人id，拼接成会话id，然后再根据会话id查询message
    @Override
    public List<Message> getMessageByCid(int to_id, User loginUser){

        //会话id是string类型的，始终是小ID在前，大id在后
        String conversionId;
        if (to_id < loginUser.getId()){
         conversionId = to_id + "_" + loginUser.getId();
        }else {
            conversionId = loginUser.getId() + "_" +to_id;
        }
//设置查询条件
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("conversation_id",conversionId);

//        开始查询
        List<Message> messageList = this.list(queryWrapper);

        return messageList;

    }
}




